package com.github.itdachen.utils;

import com.github.itdachen.constants.HtmlTypeConstants;
import com.github.itdachen.constants.JavaFieldConstants;
import com.github.itdachen.constants.PackageNameConstant;
import com.github.itdachen.entity.*;
import com.github.itdachen.framework.boot.run.handler.ContextPathHandler;
import com.github.itdachen.framework.context.BizContextHandler;
import com.github.itdachen.framework.core.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description:
 * Created by 王大宸 on 2023/04/10 22:00
 * Created with IntelliJ IDEA.
 */
public class TableColumnFieldUtils {


    public static ProtoTableInfo pottingTableInfo(List<ProtoTable> list, String appId, String uiStyle) {
        return pottingTableInfo(list, appId, uiStyle, PackageNameConstant.PARENT_MENU_ID);
    }

    public static ProtoTableInfo pottingTableInfo(List<ProtoTable> list, String appId, String uiStyle, String menuId) {
        DevTableColumn column;
        DevTableInfo tableInfo;
        List<ProtoColumns> protoColumns;
        List<DevTableColumn> columns = new ArrayList<>();
        List<DevTableInfo> tableInfos = new ArrayList<>();
        int index;
        List<String> hasColumns = new ArrayList<>();
        for (ProtoTable protoTable : list) {
            tableInfo = new DevTableInfo();
            tableInfo.setId(VelocityContextUtils.getId());
            tableInfo.setCreateTime(LocalDateTime.now());
            tableInfo.setCreateUserId(StringUtils.isEmpty(BizContextHandler.getUserId()) ? "-" : BizContextHandler.getUserId());
            tableInfo.setCreateUser(StringUtils.isEmpty(BizContextHandler.getNickName()) ? "-" : BizContextHandler.getNickName());
            tableInfo.setUpdateTime(LocalDateTime.now());
            tableInfo.setUpdateUserId(StringUtils.isEmpty(BizContextHandler.getUserId()) ? "-" : BizContextHandler.getUserId());
            tableInfo.setUpdateUser(StringUtils.isEmpty(BizContextHandler.getNickName()) ? "-" : BizContextHandler.getNickName());
            tableInfo.setRemarks("-");
            tableInfo.setPermsPrefix("-");

            /* 基础信息 */
            tableInfo.setAppId(appId);
            tableInfo.setMenuId(menuId);
            tableInfo.setTableName(protoTable.getTableName());
            tableInfo.setFuncAuthor(StringUtils.isEmpty(BizContextHandler.getNickName()) ? "<请填写开发者姓名>" : BizContextHandler.getNickName());
            tableInfo.setFuncName(protoTable.getTableComment());
            tableInfo.setClazzName(GeneratorUtils.tableToJava(protoTable.getTableName(), ""));
            tableInfo.setIframe("_component");
            tableInfo.setContextPath(ContextPathHandler.contextPath());

            /* 后端设置 */
            tableInfo.setRearPackage(GenConstants.PACKAGE_NAME);
            tableInfo.setRearSplit("0");
            tableInfo.setRearDdd("1");
            tableInfo.setRearTemp(GenConstants.TPL_BIZ);
            tableInfo.setRearUri(VelocityUtils.getControllerUri(tableInfo));
            tableInfo.setAskUri(VelocityUtils.getAskUri(tableInfo));
            tableInfo.setModuleName(GenConstants.MODULE_NAME);

            /* 后端类是否使用构建这模式 */
            tableInfo.setBuildEntity("0");
            tableInfo.setBuildDto("0");
            tableInfo.setBuildVo("0");
            tableInfo.setBuildQuery("1");

            /* 其他插件 */
            tableInfo.setUseLombok("0");
            tableInfo.setUseSwagger("0");

            /* 前端 */
            tableInfo.setFrontPackage(GenConstants.MODULE_NAME);
            tableInfo.setFrontUiStyle(uiStyle);

            tableInfos.add(tableInfo);

            /* 字段信息 */
            protoColumns = protoTable.getColumns();
            index = 1;
            hasColumns = new ArrayList<>();
            for (ProtoColumns protoColumn : protoColumns) {
                if (null == protoColumn.getColumnName() || "".equals(protoColumn.getColumnName()) || hasColumns.contains(protoColumn.getColumnName())) {
                    continue;
                }
                hasColumns.add(protoColumn.getColumnName());
                column = new DevTableColumn();
                column.setId(VelocityContextUtils.getId());
                column.setCreateTime(LocalDateTime.now());
                column.setCreateUserId(StringUtils.isEmpty(BizContextHandler.getUserId()) ? "-" : BizContextHandler.getUserId());
                column.setCreateUser(StringUtils.isEmpty(BizContextHandler.getNickName()) ? "-" : BizContextHandler.getNickName());
                column.setUpdateTime(LocalDateTime.now());
                column.setUpdateUserId(StringUtils.isEmpty(BizContextHandler.getUserId()) ? "-" : BizContextHandler.getUserId());
                column.setUpdateUser(StringUtils.isEmpty(BizContextHandler.getNickName()) ? "-" : BizContextHandler.getNickName());
                column.setTableId(tableInfo.getId());
                column.setColumnName(protoColumn.getColumnName());
                column.setColumnComment(StringUtils.isEmpty(protoColumn.getColumnComment()) ? protoColumn.getColumnName() : protoColumn.getColumnComment());
                column.setColumnType(protoColumn.getDataType());
                column.setSort(index);
                column.setDictType("VALID_FLAG");
                column.setIsPk("0");
                if ("PRI".equals(protoColumn.getColumnKey()) || "P".equals(protoColumn.getColumnKey())) {
                    column.setIsPk("1");
                }
                TableColumnFieldUtils.initColumnField(column);
                columns.add(column);
                index++;
            }
        }

        return new ProtoTableInfo(tableInfos, columns);
    }

    /***
     * 初始化表类型
     *
     * @author 王大宸
     * @date 2023/4/10 22:01
     * @param column column
     * @return void
     */
    private static void initColumnField(DevTableColumn column) {
        // 默认不需要
        column.setIsList(GenConstants.NOT_REQUIRE);
        column.setIsQuery(GenConstants.NOT_REQUIRE);
        column.setIsVo(GenConstants.NOT_REQUIRE);
        column.setIsDto(GenConstants.NOT_REQUIRE);
        column.setIsForm(GenConstants.NOT_REQUIRE);
        column.setIsRequired(GenConstants.NOT_REQUIRE);
        column.setIsExp(GenConstants.NOT_REQUIRE);

        /* 默认使用 */
        column.setIsUsable(GenConstants.REQUIRE);

        // 数据类型
        String columnName = column.getColumnName();
        // 设置java字段名
        column.setJavaField(GeneratorUtils.toCamelCase(columnName));
        // 设置默认类型
        column.setJavaType(JavaFieldConstants.STRING);
        column.setQueryType(GenConstants.QUERY_EQ);
        column.setHtmlType(HtmlTypeConstants.INPUT);
        column.setTsType(JavaFieldConstants.STRING.toLowerCase());

        /* 字段设置 */
        TableFieldUtils.setTableField(column);

        // 表单字段
        if (!arraysContains(GenConstants.FIELD_NOT_FORM, columnName)) {
            column.setIsForm(GenConstants.REQUIRE);
        }
        // 必填字段
        if (!arraysContains(GenConstants.COLUMN_NAME_NOT_REQUIRED, columnName)) {
            column.setIsRequired(GenConstants.REQUIRE);
        }
        // 列表字段
        if (!arraysContains(GenConstants.COLUMN_NAME_NOT_LIST, columnName) && "0".equals(column.getIsPk())) {
            column.setIsList(GenConstants.REQUIRE);
            column.setIsExp(GenConstants.REQUIRE);
        }
        // 查询字段
        if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && "0".equals(column.getIsPk())) {
            column.setIsQuery(GenConstants.REQUIRE);
        }
        // dto/vo 字段
        if (!arraysContains(GenConstants.USER_FIELD, columnName)) {
            column.setIsDto(GenConstants.REQUIRE);
        }
        if (!arraysContains(GenConstants.USER_FIELD, columnName)) {
            column.setIsVo(GenConstants.REQUIRE);
        }

        // 查询字段类型
        if (StringUtils.endsWithIgnoreCase(columnName, "name") || StringUtils.endsWithIgnoreCase(columnName, "title")) {
            column.setQueryType(GenConstants.QUERY_LIKE);
        }
    }

    private static boolean arraysContains(String[] arr, String targetValue) {
//        String lowerCase = targetValue.toLowerCase();
//        List<String> list = Arrays.asList(arr);
//        if (list.isEmpty()) {
//            return false;
//        }
//        for (int i = 0; i < list.size(); i++) {
//            if (lowerCase.equals(list.get(i))) {
//                return true;
//            }
//        }
//        return false;
       return Arrays.asList(arr).contains(targetValue);
    }


}
